package com.zyh.stream;

import com.zyh.domain.Student;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author zyh
 * @Date 2024/8/16 14:51
 * @Description 归约
 */

public class StreamDemo6 {
    @Test
    public void test1(){
        List<Student> list = new ArrayList<>();
        list.add(new Student("zyh", "男", 18, 170.5));
        list.add(new Student("wy", "男", 20, 140.5));
        list.add(new Student("cxk", "女", 45, 120.5));
        list.add(new Student("dz", "女", 2, 180.5));
        list.add(new Student("zhangSan", "男", 13, 146.5));
        list.add(new Student("li4", "女", 56, 189.5));
        list.add(new Student("wang5", "男", 24, 110.5));
        //将所有学生的年龄累加求和
        Integer sumAge = list.stream().map(i -> i.getAge()).reduce((i1, i2) -> i1 + i2).get();
        System.out.println("sumAge = " + sumAge);
        //求最高身高的值
        Double maxHeight = list.stream().map(i -> i.getHeight()).reduce((i1, i2) -> i1 > i2 ? i1 : i2).get();
        System.out.println("maxHeight = " + maxHeight);
        //求身高平均值
        Double avgHeight = list.stream().map(i -> i.getHeight()).reduce((i1, i2) -> (i1 + i2)).get();
        avgHeight=avgHeight/list.size();
        System.out.println("avgHeight = " + avgHeight);
    }
}
